﻿<?php

class DB
{
	protected static $mysqli;
	
	public static function getMysqli()
	{
		if (self::$mysqli == null)
		{
			self::$mysqli = new mysqli('localhost', 'expertenvz', 'expertenvz', 'expertenvz');
		}
		return self::$mysqli;
	}
	


	public static function LoadUser($userId)
	{
		$db = DB::getMysqli();
		if (mysqli_connect_errno()) {
			die ('Konnte keine Verbindung zur Datenbank aufbauen: '.mysqli_connect_error().'('.mysqli_connect_errno().')');
			}
		$queryStr = 'SELECT * FROM users WHERE id = '.$userId.';';
		$result = $db->query($queryStr);
		if (!$result) {
			die ('Etwas stimmte mit dem Query nicht: '.$db->error);
		}
		$row = $result->fetch_assoc();
		$user = new User($row['id'], null, $row['name'],null);
		$result->close();
		return $user;
	}
	
	
	
	public static function LoadUserQuestions($user)
	{
		$db = DB::getMysqli();
		$result = $db->query('SELECT id FROM questions WHERE user = '.$user->GetId().';');
		while ($row = $result->fetch_assoc())
		{
			$question = self::loadQuestion($row['id']);
			$user->AddQuestion($question);
		}
		$result->close();
	}
	
	
	
  public static function LoadQuestion($questionId)
  {
    $db = DB::getMysqli();
    $queryStr = 'SELECT
                   q.id AS q_id, q.title AS q_title, q.text AS q_text, q.askedBy AS q_askedBy, 
                   q.askedAt AS q_askedAt, q.user AS q_user,
                   u.id AS u_id, u.name AS u_name
                 FROM
                   questions AS q, users AS u
                 WHERE
                   q.id = '.$questionId.' AND q.askedBy = u.id;';
    $result = $db->query($queryStr);
    $row = $result->fetch_assoc();
    $noAnswer = $row;
    $result->close();
    $answer = self::LoadAnswer($questionId);
    $question = new Question($noAnswer['q_id'], $noAnswer['q_title'], $noAnswer['q_text'], null, 
                             $noAnswer['q_askedAt'], $noAnswer['q_askedBy'], $noAnswer['u_name'], 
                             $answer['text'], $answer['answeredAt'], $answer['feedback']);
    return $question;
  }
  
  
  
  public static function LoadAnswer($questionId)
  {
    $db = DB::getMysqli();
    $queryStr = 'SELECT
                   *
                 FROM
                   answers
                 WHERE
                   question = '.$questionId.';';
    $result = $db->query($queryStr);
    $row = $result->fetch_assoc();
    $result->close();
    if ($row['text'])
    {
    	return $row;
    }
    else
    {
    	return array('text'=>'', 'AnsweredAt'=>'', 'feedback'=>0);
    }
    
  }
  
  
  
  //TODO: Spalte password in Datenbank anlegen
  public static function Login($name, $password)
  {
 	session_start();
 	
  	$db = DB::getMysqli();
  	if (mysqli_connect_errno()) {
			die ('Konnte keine Verbindung zur Datenbank aufbauen: '.mysqli_connect_error().'('.mysqli_connect_errno().')');
			}
	$queryStr = 'SELECT * FROM users WHERE name = \''.$name.'\';';
	$result = $db->query($queryStr);
  		if (!$result) {
			die ('Etwas stimmte mit dem Query nicht: '.$db->error);
		}
	$row = $result->fetch_assoc();
	$result->close();
	
	if(!empty($row))
	{
		if($row['password'] == md5($password))
		{
			$_SESSION['id'] = $row['id'];
			$_SESSION['name'] = $row['name'];
			
			return true;
		}
	}

	return false;	
	
  }
  
  
  
}
?>